<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .slider-bar {
            position: absolute;
            left: 50%;
            top: 300px;
            margin-left: 600px;
            width: 45px;
            height: 130px;
            background-color: pink;
        }
        
        .w {
            width: 1200px;
            margin: 10px auto;
        }
        
        .header {
            height: 150px;
            background-color: purple;
        }
        
        .banner {
            height: 250px;
            background-color: skyblue;
        }
        
        .main {
            height: 1000px;
            background-color: yellowgreen;
        }
        
        span {
            display: none;
            position: absolute;
            bottom: 0;
        }
    </style>
    <script>
        window.addEventListener('load', function() {
            var sliderbar = document.querySelector('.slider-bar');
            var header = document.querySelector('.header');
            var banner = document.querySelector('.banner');
            var main = document.querySelector('.main');
            var goback = document.querySelector('.goBack')
                //页面滚动事件
            var bannerTop = banner.offsetTop;
            var sliderbarTop = sliderbar.offsetTop - bannerTop;
            var mainTop = main.offsetTop;


            document.addEventListener('scroll', function() {
                console.log(window.pageYoffset);
                if (window.pageYOffset >= bannerTop) {
                    sliderbar.style.position = 'fixed';
                    sliderbar.style.top = sliderbarTop + 'px';
                } else {
                    sliderbar.style.position = 'absolute';
                    sliderbar.style.top = '300px';
                }
                if (window.pageYOffset >= mainTop) {
                    goback.style.display = 'block';
                } else {
                    goback.style.display = 'none';
                }

            })
        })
    </script>
</head>

<body>
    <div class="slider-bar">
        <span class="goBack">返回顶部</span>
    </div>
    <div class="header w">头部区域</div>
    <div class="banner w">banner区域</div>
    <div class="main w">主体部分</div>
</body>

</html>